/*
Copyright 2010 Iain Surgey.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
msurface_t		*r_skyfaces;
mplane_t		r_skyplanes[6];
mtexinfo_t		r_skytexinfo[6];
mvertex_t		*r_skyverts;
medge_t			*r_skyedges;
int				*r_skysurfedges;

int skybox_planes[12];
int box_surfedges[24];
int box_edges[24];
int	box_faces[6];
float box_vecs[12][3];
float box_verts[8][3];

model_t skymodel;

int r_skyframe;

#__body__

/*
================
R_EmitSkyBox
================
*/
private function R_EmitSkyBox ()
{
	#{d| int		i, j}
	#{d| int		oldkey}
	
	currentmodel = #{&models[0]};
	
	if (r_skyframe == r_framecount)
		return;		// already set this frame

	r_skyframe = r_framecount;

	// set the eight fake vertexes
	for (i in 0...8)
		for (j in 0...3)
			#{r_skyverts[i].position.p[j] = r_origin[j] + box_verts[i][j]*128};

	// set the six fake planes
	for (i in 0...6)
		if (#{skybox_planes[i*2+1]} > 0)
			#{r_skyplanes[i].dist = r_origin[skybox_planes[i*2]]+128};
		else
			#{r_skyplanes[i].dist = r_origin[skybox_planes[i*2]]-128};
	
	// fix texture offseets
	for (i in 0...6)
	{
		#{r_skytexinfo[i].vecs[0][3] = #[-DotProduct (]#r_origin#[,]# r_skytexinfo[i].vecs[0])};
		#{r_skytexinfo[i].vecs[1][3] =  #[-DotProduct (]#r_origin#[,]# r_skytexinfo[i].vecs[1])};
	}

	// emit the six faces
	oldkey = r_currentkey;
	r_currentkey = 0x7ffffff0;
 	for (i in 0...6)
	{
		R_RenderFace (#{&r_skyfaces[i]}, 15);
	}
	r_currentkey = oldkey;		// bsp sorting order
}


/*
================
R_InitSkyBox

================
*/
private function R_InitSkyBox ()
{
	currentmodel = #{&models[0]};
	LoadSkyData();
	#{r_skyfaces = currentmodel->surfaces + (currentmodel->numsurfaces*sizeof(msurface_t))};
	#{currentmodel->numsurfaces = currentmodel->numsurfaces + 6};
	#{r_skyverts = currentmodel->vertexes + (currentmodel->numvertexes*sizeof(mvertex_t))};
	#{currentmodel->numvertexes = currentmodel->numvertexes + 8};
	#{r_skyedges = currentmodel->edges + (currentmodel->numedges*sizeof(medge_t))};
	#{currentmodel->numedges = currentmodel->numedges + 12};
	#{r_skysurfedges = currentmodel->surfedges + (currentmodel->numsurfedges*4)};
	#{currentmodel->numsurfedges = currentmodel->numsurfedges + 24};
	
	for (i in 0...6)
	{
		#{r_skyplanes[i].normal.p[skybox_planes[i*2]] = 1};
		#{r_skyplanes[i].dist = skybox_planes[(i*2)+1]};

		VectorCopy (#{box_vecs[i*2]}, #{r_skytexinfo[i].vecs[0]});
		VectorCopy (#{box_vecs[(i*2)+1]}, #{r_skytexinfo[i].vecs[1]});
		
		#{r_skyfaces[i].plane = &r_skyplanes[i]};
		#{r_skyfaces[i].numedges = 4};
		#{r_skyfaces[i].flags = box_faces[i] | #[SURF_DRAWSKYBOX]#};
		#{r_skyfaces[i].firstedge = currentmodel->numsurfedges-24+i*4};
		#{r_skyfaces[i].texinfo = &r_skytexinfo[i]};
		#{r_skyfaces[i].texturemins[0] = -128};
		#{r_skyfaces[i].texturemins[1] = -128};
		#{r_skyfaces[i].extents[0] = 256};
		#{r_skyfaces[i].extents[1] = 256};
	}

	for (i in 0...24)
		if (#{box_surfedges[i]} > 0)
			#{r_skysurfedges[i] = currentmodel->numedges-13 + box_surfedges[i]};
		else
			#{r_skysurfedges[i] = -(currentmodel->numedges-13 + -box_surfedges[i])};

	for(i in 0...12)
	{
		#{r_skyedges[i].v[0] = currentmodel->numvertexes-9+box_edges[i*2]};
		#{r_skyedges[i].v[1] = currentmodel->numvertexes-9+box_edges[i*2+1]};
		#{r_skyedges[i].cachededgeoffset = 0};
	}
	
	R_SetSky();
}


/*
============
R_SetSky
============
*/
private function R_SetSky ()
{
	#{d| int i}

	for (i in 0...6)
	{
		#{r_skytexinfo[i].image = #[R_FindImage ("sky_"+i, IT_SKY)]#};
	}
}



/*
================
R_InitSkyBox

================
*/
private function LoadSkyData ()
{
	#{d| int		i}
	
	#{skybox_planes[0] = 2};
	#{skybox_planes[1] = -128};
	#{skybox_planes[2] = 0};
	#{skybox_planes[3] = -128};
	#{skybox_planes[4] = 2};
	#{skybox_planes[5] = 128};
	#{skybox_planes[6] = 1};
	#{skybox_planes[7] = 128};
	#{skybox_planes[8] = 0};
	#{skybox_planes[9] = 128};
	#{skybox_planes[10] = 1};
	#{skybox_planes[11] = -128};
	
	#{box_surfedges[0] = 1};
	#{box_surfedges[1] = 2};
	#{box_surfedges[2] = 3};
	#{box_surfedges[3] = 4};
	#{box_surfedges[4] = -1};
	#{box_surfedges[5] = 5};
	#{box_surfedges[6] = 6};
	#{box_surfedges[7] = 7};
	#{box_surfedges[8] = 8};
	#{box_surfedges[9] = 9};
	#{box_surfedges[10] = -6};
	#{box_surfedges[11] = 10};
	#{box_surfedges[12] = -2};
	#{box_surfedges[13] = -7};
	#{box_surfedges[14] = -9};
	#{box_surfedges[15] = 11};
	#{box_surfedges[16] = 12};
	#{box_surfedges[17] = -3};
	#{box_surfedges[18] = -11};
	#{box_surfedges[19] = -8};
	#{box_surfedges[20] = -12};
	#{box_surfedges[21] = -10};
	#{box_surfedges[22] = -5};
	#{box_surfedges[23] = -4};
	
	#{box_edges[0] = 1};
	#{box_edges[1] = 2};
	#{box_edges[2] = 2};
	#{box_edges[3] = 3};
	#{box_edges[4] = 3};
	#{box_edges[5] = 4};
	#{box_edges[6] = 4};
	#{box_edges[7] = 1};
	#{box_edges[8] = 1};
	#{box_edges[9] = 5};
	#{box_edges[10] = 5};
	#{box_edges[11] = 6};
	#{box_edges[12] = 6};
	#{box_edges[13] = 2};
	#{box_edges[14] = 7};
	#{box_edges[15] = 8};
	#{box_edges[16] = 8};
	#{box_edges[17] = 6};
	#{box_edges[18] = 5};
	#{box_edges[19] = 7};
	#{box_edges[20] = 8};
	#{box_edges[21] = 3};
	#{box_edges[22] = 7};
	#{box_edges[23] = 4};
	
	#{box_faces[0] = 0};
	#{box_faces[1] = 0};
	#{box_faces[2] = 2};
	#{box_faces[3] = 2};
	#{box_faces[4] = 2};
	#{box_faces[5] = 0};
	
	VectorSet(#{box_vecs[0]},0,-1,0);
	VectorSet(#{box_vecs[1]},-1,0,0);
	VectorSet(#{box_vecs[2]},0,1,0);
	VectorSet(#{box_vecs[3]},0,0,-1);
	VectorSet(#{box_vecs[4]},0,-1,0);
	VectorSet(#{box_vecs[5]},1,0,0);
	VectorSet(#{box_vecs[6]},1,0,0);
	VectorSet(#{box_vecs[7]},0,0,-1);
	VectorSet(#{box_vecs[8]},0,-1,0);
	VectorSet(#{box_vecs[9]},0,0,-1);
	VectorSet(#{box_vecs[10]},-1,0,0);
	VectorSet(#{box_vecs[11]},0,0,-1);
	
	#{box_verts[0][0] = -1};
	#{box_verts[0][1] = -1};
	#{box_verts[0][2] = -1};
	#{box_verts[1][0] = -1};
	#{box_verts[1][1] = 1};
	#{box_verts[1][2] = -1};
	#{box_verts[2][0] = 1};
	#{box_verts[2][1] = 1};
	#{box_verts[2][2] = -1};
	#{box_verts[3][0] = 1};
	#{box_verts[3][1] = -1};
	#{box_verts[3][2] = -1};
	#{box_verts[4][0] = -1};
	#{box_verts[4][1] = -1};
	#{box_verts[4][2] = 1};
	#{box_verts[5][0] = -1};
	#{box_verts[5][1] = 1};
	#{box_verts[5][2] = 1};
	#{box_verts[6][0] = 1};
	#{box_verts[6][1] = -1};
	#{box_verts[6][2] = 1};
	#{box_verts[7][0] = 1};
	#{box_verts[7][1] = 1};
	#{box_verts[7][2] = 1};

}


